<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Api extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
        $this->load->helper('api');

        /* $aigs = json_decode(file_get_contents('php://input'),true);
        //$aigs = $this->input->raw_input_stream;

        $path = $aigs['sfp']; */
        $path = $_FILES['sfp'];
        //var_dump($path);
        //file_put_contents('/abc.m4a',$path);

        $data   = file_get_contents($path['tmp_name']);
        $base64 = base64_encode($data);

        // 设置请求数据
        $appkey = 'KaWm6DuG6RkeSMT7';
        $params = array(
            'app_id'       => '1106674506',
            'format'       => '2',
            'rate'         => '16000',
            'speech'       => $base64,
            'time_stamp'   => strval(time()),
            'nonce_str'    => strval(rand()),
            'sign'         => '',
        );
        $params['sign'] = getReqSign($params, $appkey);
        $appkey = 'KaWm6DuG6RkeSMT7';
        $url = 'https://api.ai.qq.com/fcgi-bin/aai/aai_asr';
        $response = doHttpPost($url, $params);
        $res = json_decode($response,true);

        echo $res['data']['text'];
	}
}
